package cmcshan1;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class VisitTableView extends JPanel implements ActionListener{

	private Driver driver;
	public ArrayList<String[]> visitTable = new ArrayList<String[]>();
	private JPanel addPanel;
	private JTextField pidField;
	private JTextField timeField;
	private JTextField dateField;
	private JTextField nidField;
	private JTextField didField;
	private JTextField bldprssField;
	private JTextField bodytempField;
	private JTextField pulseField;
	private JTextField symptomsField;
	private JTextField testorderedField;
	private JTextField testresultField;
	private JTextField diagnosisField;
	private JButton addButton;
	private JPanel tablePanel;

	public VisitTableView(Driver _driver){
		driver = _driver;
		setPreferredSize(new Dimension(1000, 400));
		//setBorder(BorderFactory.createLineBorder(Color.black));
		GridLayout tableLayout = new GridLayout(0,1);
		
		//Adding text fields to add new patients
		addPanel = new JPanel();
		addPanel.setPreferredSize(new Dimension(950, 60));
		addPanel.setLayout(new GridLayout(0, 12));
		
		//1st row of addpanel
		JLabel pidtext = new JLabel("Patient ID:");
		addPanel.add(pidtext);
		JLabel timetext = new JLabel("Time:");
		addPanel.add(timetext);
		JLabel datetext = new JLabel("Date:");
		addPanel.add(datetext);
		JLabel nidtext = new JLabel("Nurse ID:");
		addPanel.add(nidtext);
		JLabel didtext = new JLabel("Doctor ID:");
		addPanel.add(didtext);
		JLabel bldprsstext = new JLabel("Blood Pressure:");
		addPanel.add(bldprsstext);
		JLabel bodytemptext = new JLabel("Temperature:");
		addPanel.add(bodytemptext);
		JLabel pulsetext = new JLabel("Pulse:");
		addPanel.add(pulsetext);
		JLabel symptomstext = new JLabel("Symptoms:");
		addPanel.add(symptomstext);
		JLabel testorderedtext = new JLabel("Test Ordered:");
		addPanel.add(testorderedtext);
		JLabel testresulttext = new JLabel("Test Result:");
		addPanel.add(testresulttext);
		JLabel diagnosistext = new JLabel("Diagnosis:");
		addPanel.add(diagnosistext);
		
		//2nd row of addpanel
		pidField = new JTextField(5);
		addPanel.add(pidField);
		timeField = new JTextField(5);
		addPanel.add(timeField);
		dateField = new JTextField(5);
		addPanel.add(dateField);
		nidField = new JTextField(5);
		addPanel.add(nidField);
		didField = new JTextField(5);
		addPanel.add(didField);
		bldprssField = new JTextField(5);
		addPanel.add(bldprssField);
		bodytempField = new JTextField(5);
		addPanel.add(bodytempField);
		pulseField = new JTextField(5);
		addPanel.add(pulseField);
		symptomsField = new JTextField(5);
		addPanel.add(symptomsField);
		testorderedField = new JTextField(5);
		addPanel.add(testorderedField);
		testresultField = new JTextField(5);
		addPanel.add(testresultField);
		diagnosisField = new JTextField(5);
		addPanel.add(diagnosisField);
		
		addButton = new JButton("Add");
		addPanel.add(addButton);
		this.add(addPanel);
		
		//initializing add button
		addButton.setActionCommand("add");
		addButton.addActionListener(this);
		tablePanel = new JPanel();
		tablePanel.setLayout(tableLayout);
		this.add(tablePanel);
		
		
		Connection con = null;
        Statement stmt = null;
        try 
		{   //load JDBC driver
			Class.forName("com.mysql.jdbc.Driver");
		    //connect to the DB
			String connectionString = "jdbc:mysql://cs.westga.edu:3307/cmcshan1?user=cmcshan1&password=cm@3274";
            con = (Connection) DriverManager.getConnection(connectionString);
		    //execute SQL statement
			stmt = (Statement) con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from VISITS");
            while (rs.next()) {
				//System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6));
            	String[] visit = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
            	visitTable.add(visit);
            }
            initializeTable(visitTable);
        }
        catch (Exception e) 
        {
            System.out.println(e.toString());
        }
        
        JButton resetButton = new JButton("Reset");
		resetButton.addActionListener(this);
		resetButton.setActionCommand("reset");
		this.add(resetButton);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand() == "add"){
			String s1 = pidField.getText();
			String s2 = timeField.getText();
			String s3 = dateField.getText();
			String s4 = nidField.getText();
			String s5 = didField.getText();
			String s6 = bldprssField.getText();
			String s7 = bodytempField.getText();
			String s8 = pulseField.getText();
			String s9 = symptomsField.getText();
			String s10 = testorderedField.getText();
			String s11 = testresultField.getText();
			String s12 = diagnosisField.getText();
			String[] sArray = {s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12};
			
			Connection con = null;
	        Statement stmt = null;
	        try 
			{   //load JDBC driver
				Class.forName("com.mysql.jdbc.Driver");
			    //connect to the DB
				String connectionString = "jdbc:mysql://cs.westga.edu:3307/cmcshan1?user=cmcshan1&password=cm@3274";
	            con = (Connection) DriverManager.getConnection(connectionString);
			    //execute SQL statement
				stmt = (Statement) con.createStatement();
	            stmt.executeUpdate("insert into visits values (" + sArray[0] + ", '" + sArray[1] + "', '" + sArray[2] + "', " + sArray[3] + ", " + sArray[4] + ", " + sArray[5] + ", " + sArray[6] + ", " + sArray[7] + ", '" + sArray[8] + "', '" + sArray[9] + "', '" + sArray[10] + "', '"  + sArray[11] + "');");
	            visitTable.add(sArray);
				initializeTable(visitTable);
	        }
	        catch (Exception exc) 
	        {
	            System.out.println(exc.toString());
	        }
		}
		
		if(e.getActionCommand() == "reset"){
			visitTable.clear();
			
			Connection con = null;
	        Statement stmt = null;
	        try 
			{   //load JDBC driver
				Class.forName("com.mysql.jdbc.Driver");
			    //connect to the DB
				String connectionString = "jdbc:mysql://cs.westga.edu:3307/cmcshan1?user=cmcshan1&password=cm@3274";
	            con = (Connection) DriverManager.getConnection(connectionString);
			    //execute SQL statement
				stmt = (Statement) con.createStatement();
	            ResultSet rs = stmt.executeQuery("select * from VISITS");
	            while (rs.next()) {
					//System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6));
	            	String[] visit = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
	            	visitTable.add(visit);
	            }
	            initializeTable(visitTable);
	        }
	        catch (Exception exc)
	        {
	        	System.out.println(exc.toString());
	        }
		}
		
	}
	
	public void initializeTable(ArrayList<String[]> tableToDraw){
		tablePanel.removeAll();
		for(int i = 0; i < tableToDraw.size(); i++){
			String[] tableColumn = tableToDraw.get(i);
			JPanel panel = new JPanel();
			GridLayout pLayout = new GridLayout(1, 12);
			panel.setLayout(pLayout);
			panel.setPreferredSize(new Dimension(950, 30));
			for(int x = 0; x < tableColumn.length; x++){
				JLabel label = new JLabel(tableColumn[x]);
				//label.setBorder(BorderFactory.createLineBorder(Color.black));
				panel.add(label);
			}
			tablePanel.add(panel);
		}
		this.repaint();
		this.revalidate();
	}

}